/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.base.hsqldb;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.webos.java.base.Session;
import org.webos.java.base.fs.DBFileAccess;
import org.webos.java.base.fs.DBFileRAFile;
import org.webos.java.root.Log;

/**
 *
 * @author Kostya
 */
public class HSQLDBConnection {

    private String url = "";
    private String userName = "";
    private String password = "";
    private Connection connection = null;
    private boolean active = false;

    public HSQLDBConnection(String aURL, String aUserName, String aPassword) {
        url = aURL;
        userName = aUserName;
        password = aPassword;
    }

    public boolean openConnection(Session aSession){
        return openConnection(aSession, true);
    }
    public boolean openConnection(Session aSession, boolean aAutoCommit){
        try {
            if(connection!=null && !connection.isClosed())
                return true;
            Class.forName("org.hsqldb.jdbcDriver");
            Properties p = new Properties();
            p.setProperty("user", userName);
            p.setProperty("password", password);
            p.setProperty("shutdown", "true");
            p.setProperty("fileaccess_class_name", DBFileAccess.class.getCanonicalName());
            p.setProperty("storage_class_name", DBFileRAFile.class.getCanonicalName());
            p.setProperty("storage_key", aSession.getId());
//            Log.debug("HSQLDBConnection", "openConnection: fileaccess_class_name = "+
//                    p.getProperty("fileaccess_class_name")+", key = "+p.getProperty("storage_key"));
            connection = DriverManager.getConnection("jdbc:hsqldb:file:"+aSession.getId()+"/"+url, p);
            connection.setAutoCommit(aAutoCommit);
            active = true;
            return true;
        } catch (Throwable t) {
            Log.error("HSQLDBConnection", "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return false;
    }

    public Connection getConnection() {
        return connection;
    }

    public void close(){
        if(!active)
            return;
        try {
            connection.close();
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        connection = null;
        active = false;
    }

    public int getLastID(){
        try {
            if(!isActive())
                return -1;
            PreparedStatement st = getConnection().prepareStatement("CALL IDENTITY()");
            ResultSet rset = st.executeQuery();
            if(rset.next())
                return rset.getInt(1);
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return -1;
    }

    public boolean isActive() {
        return active;
    }

    public PreparedStatement prepareQuery(String aQuery) throws SQLException{
        return getConnection().prepareStatement(aQuery);
    }

    public void commit(){
        try {
            if(isActive()){
                getConnection().commit();
            }
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
    }

    public void rollback(){
        try {
            if(isActive()){
                getConnection().rollback();
            }
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
    }
}
